package com.ymsoft.myerp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ymsoft.myerp.common.BaseController;
import com.ymsoft.myerp.common.DTO;
import com.ymsoft.myerp.common.RetEnum;
import com.ymsoft.myerp.entity.Role;
import com.ymsoft.myerp.service.IRoleService;
import com.ymsoft.myerp.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ymsoft
 * @since 2020-04-12
 */
@Controller
@RequestMapping("/myerp/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService iRoleService;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(@RequestParam(required = false) Integer id,
                                  @RequestParam(required = false) String name,
                                  @RequestParam int page, @RequestParam int limit) {//layui默认分页参数page和limit
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        if(id != null) {
            queryWrapper.eq("id", id);
        }
        queryWrapper.orderByDesc("id");
        // 查询第1页，每页返回5条
        Page<Role> userPage = new Page<>(page, limit);
        IPage<Role> iPage = iRoleService.page(userPage, queryWrapper);
        //需要配置mybatis plus的分页插件，否则无法正常分页
        return ResponseEntity.ok(DTO.retSuccess(iPage.getRecords(), (int)iPage.getTotal()));
    }


    @RequestMapping("/all")
    @ResponseBody
    public DTO<?> all() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        List<Role> list = iRoleService.list(queryWrapper);
        return DTO.retSuccess(list);
    }

    @PostMapping("/save")
    @ResponseBody
    public DTO<?> add(@Valid Role role) {
        role.setId(null);
        boolean isOk = iRoleService.save(role);
        if(isOk) {
            return DTO.retSuccess();
        }
        return DTO.retFailed();
    }

    @PostMapping("/update")
    @ResponseBody
    public DTO<?> update(@Valid Role role) {
        boolean isOk = iRoleService.updateById(role);
        if(isOk) {
            return DTO.retSuccess();
        }
        return DTO.retFailed();
    }

    @PostMapping("/delete")
    @ResponseBody
    public DTO<?> delete(@RequestParam Integer id) {
        Role role = iRoleService.getById(id);
        if(role != null && !"ADMIN".equals(role.getName())) {
            boolean isOk = iRoleService.removeById(id);
            if(isOk) return DTO.retSuccess();
        } else if(role != null && "ADMIN".equals(role.getName())) {
            return DTO.ret(RetEnum.RET_COMM_ADMIN_ROLE_FORBIDDEN_TO_DELETE);
        }
        return DTO.retFailed();
    }
}
